package application.common.util;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.jfinal.ext.kit.DateKit;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;

public class S3Util {

	private final static Log log = Log.getLog(S3Util.class);
	private static final String accessKey = "5A6CL1WZ7CQ488FWGJ01";
	private static final String secretKey = "2mvQNzT4McAcXhLsoOIOWSMrXMM2yEnrLwYkF61p";
	private static final String bucketName = "cabinetvideo";
	private static final String serviceEndpoint = "86.1.51.250";
	private static final String signingRegion = "";
	
	public static void upload() throws ParseException {
		
		if (ShiroUtil.getConfig() != null && StrKit.equals("1", ShiroUtil.getConfig().getIsVideotape())
				&& !StrKit.isBlank(ShiroUtil.getConfig().getVideotapePath())) {
			File videotapePath = new File(ShiroUtil.getConfig().getVideotapePath());
			if (videotapePath.exists()) {
				File[] files = videotapePath.listFiles();
				if (files != null) {
					ClientConfiguration clientConfig = new ClientConfiguration();
					clientConfig.setProtocol(Protocol.HTTP);
					clientConfig.setSignerOverride("S3SignerType");
					AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
							.withCredentials(new AWSStaticCredentialsProvider( new BasicAWSCredentials(accessKey, secretKey)))
							.withClientConfiguration(clientConfig)
							.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(serviceEndpoint,signingRegion))
							.withPathStyleAccessEnabled(true).build();
					
					for (File folder : files) {
						if (folder.isDirectory()) {
							String[] childrens = folder.list();
							if(childrens!=null && childrens.length>0) {
								for(String fileName:childrens) {
									File file=new File(ShiroUtil.getConfig().getVideotapePath() + "//"+folder.getName()+"//" +fileName);
									if(file.exists() && !file.isDirectory()) {
										try {
											PutObjectRequest request = new PutObjectRequest(bucketName,ShiroUtil.getConfig().getId()+"/"+folder.getName()+'/'+fileName, file);
											s3Client.putObject(request);
											file.delete();
										} catch (AmazonServiceException exception) {
								            log.error("Caught an AmazonServiceException, " +
								                    "which means your request made it " +
								                    "to Amazon S3, but was rejected with an error response " +
								            "for some reason.");
								            log.error("Error Message: " + exception.getMessage());
								            log.error("HTTP  Code: "    + exception.getStatusCode());
								            log.error("AWS Error Code:" + exception.getErrorCode());
								            log.error("Error Type:    " + exception.getErrorType());
								            log.error("Request ID:    " + exception.getRequestId());
								        } catch (AmazonClientException ace) {
								            log.error("Caught an AmazonClientException, " +
								                    "which means the client encountered " +
								                    "an internal error while trying to communicate" +
								                    " with S3, " +
								            "such as not being able to access the network.");
								            log.error("Error Message: " + ace.getMessage());
								        }
									}
								}
							}else {
								SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
								Date date1 = sdf.parse(folder.getName());
								Date date2 = sdf.parse(DateKit.toStr(new Date(System.currentTimeMillis()), "yyyy-MM-dd"));
								if (date1.before(date2)) {
									folder.delete();
								}
							}
						}
					}
					s3Client.shutdown();
				}
			}
		}
	}
	
	public static void main(String[] args) throws ParseException {
		
		File file=new File("D:\\2021-09-17");
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date1 = sdf.parse(file.getName());
		Date date2 = sdf.parse(DateKit.toStr(new Date(System.currentTimeMillis()), "yyyy-MM-dd"));
		if (date1.before(date2)) {
			System.out.println("dd");
			file.delete();
		}else {
			System.out.println("today");
		}
	}

}
